package love.yuanmengrobot.qdkaoqin.business;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.NetworkOnMainThreadException;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Timer;
import java.util.TimerTask;

import love.yuanmengrobot.qdkaoqin.R;
import love.yuanmengrobot.qdkaoqin.entity.Member;
import love.yuanmengrobot.qdkaoqin.util.Constants;
import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class PersonaCenter extends AppCompatActivity {

    private String memberId;
    private Member member;
    TextView name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_center);

        memberId=getIntent().getStringExtra("memberid");
        name=findViewById(R.id.personalName);
        ImageView img=findViewById(R.id.image);
        if(memberId==null){
            name.setText("未登录");
            img.setImageResource(0);
            return;
        }
        final String memberid=memberId;
        get(memberid);
//        name.setText(member.getName());
    }

    private okhttp3.Callback callback = new okhttp3.Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
//            Log.e(TAG, "Failed to connect server!");
            e.printStackTrace();
        }
        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                final String body = response.body().string();
                System.out.println("--------------"+body);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Gson gson = new Gson();
                        Type jsonType =////////****
                                new TypeToken<Member>() {
                                }.getType();
                        member = gson.fromJson(body, jsonType);
                        System.out.println(member);
                        name.setText(member.getName());

                    }
                });
            } else {

            }
        }
    };
    public void get(final String memberid) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                FormBody.Builder builder = new FormBody.Builder();
                /* 添加四个参数 */
                builder.add("memberid", memberid);
                FormBody body = builder.build();
                Request request = new Request.Builder().url(Constants.GETMEMBER_URL).post(body).build();

                try {
                    OkHttpClient client = new OkHttpClient();
                    client.newCall(request).enqueue(callback);
                } catch (NetworkOnMainThreadException ex) {

                    ex.printStackTrace();
                }
            }
        }).start();
    }
}
